﻿package Banner {
	import flash.net.*;
	import flash.events.*;
	
	// A wrapper class to load an XML file without caching and get its contents.
	public class XMLFileLoader {
		// The XML filename and object.
		private var xmlfilename:String;
		private var xmlfile:XML;
		private var xmlloader:URLLoader;
		private var xmlrequest:URLRequest;
		
		private var gallerySize:Number;
		private var galleryPtr:Number;
		private var galleryFiles:XMLList;

		// Filename attribute variables
		public var gFileSrc, gFileFullsize:String;
		public var gFileTitle:String;
		public var gFileWidth, gFileHeight:Number;
		
		private var loadedCallback;

		// Check if we need to load a web XML file or local one.
		function onXMLRead(e:Event)  {
			var urlVars;
			urlVars = e.target.data;
			XML.ignoreWhitespace = true;
			xmlfile = new XML(urlVars);

			// Set the XML file pointer to the first element.
			galleryFiles = xmlfile.children();
			gallerySize = galleryFiles.length();
			
			if(loadedCallback != null)
				loadedCallback(this);
		}
		
		// If we can't load the file, maybe we need a local load.
		function onXMLReadAborted(httpse:HTTPStatusEvent)  	{
			trace("HTTP Event called:" + httpse);
		}
	
		// If we can't load the file, maybe we need a local load.
		function onXMLReadSAborted(see:SecurityErrorEvent)  	{
			trace("Security Event called:" + see);
		}
		
		// If we can't load the file, maybe we need a local load.
		function onXMLReadIOAborted(ioe:IOErrorEvent)  	{
			trace("IO Event called:" + ioe);

			// Probably need to load local file.
			if(xmlfilename)	{
				xmlloader.dataFormat = URLLoaderDataFormat.TEXT;
				xmlrequest = new URLRequest(xmlfilename);
				xmlfilename = null;
				xmlloader.load(xmlrequest);
			}
		}
	
		// See if the XML file is local or on the server.
		public function XMLFileLoader(filename:String, callback) 		{ 
			gFileSrc = null;
			gFileWidth = gFileHeight = -1;
			galleryPtr = -1;			
			gallerySize = 0;
			loadedCallback = null;
			
			loadedCallback = callback;
			
			if(filename==null)
				trace("The filename is empty.");
			else {
				xmlfilename = filename;
				
				// Load the XML file.
				xmlrequest = new URLRequest(xmlfilename + "?urlloaduncache=" + new Date().getTime());
				xmlloader = new URLLoader();
				xmlloader.addEventListener(Event.COMPLETE, onXMLRead);
				xmlloader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onXMLReadAborted);
				xmlloader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onXMLReadSAborted);
				xmlloader.addEventListener(IOErrorEvent.IO_ERROR, onXMLReadIOAborted);
				xmlloader.dataFormat = URLLoaderDataFormat.TEXT;
				
				xmlloader.load(xmlrequest);
			}
		}
	
		// Get the number of items in the Gallery.
		public function GetGallerySize():Number	{
			return gallerySize;
		}
	
		// Get the next filename in the list and its properties, returns the node name.
		public function GetNextFilename():String	{
			galleryPtr++;
			if(galleryPtr>=gallerySize)
				return null;
			
			gFileSrc = galleryFiles[galleryPtr].@src;
			gFileHeight = Number(galleryFiles[galleryPtr].@height);
			gFileWidth =  Number(galleryFiles[galleryPtr].@width);
			gFileFullsize = galleryFiles[galleryPtr].@fullsize;
			gFileTitle = galleryFiles[galleryPtr].@title;
			return galleryFiles[galleryPtr].name();
		}
		
		// Set the pointer to the first filename in the list.
		public function SetFirstFilename()	{
			galleryPtr = -1;
		}

		public function registerLoadedCallback(callback)	{
			loadedCallback = callback;
		}
		
		// How many random element swaps to perform.
		public function Random()	{
			var tmpXML:XML;
			var rnd:Number;
			
			for(var i=0; i<gallerySize; i++)
			{
				rnd = (int)(Math.random() * gallerySize);

				tmpXML = galleryFiles[i];
				galleryFiles[i] = galleryFiles[rnd];
				galleryFiles[rnd] = tmpXML;
			}
		}
	}
	
}